﻿using MediatR;
using ProductionLine.Domain.Team;
using Sale.Api.Application.Command.Contract;
using Sale.Domain;
using Sale.Dtos;
using Sale.ErrorCode;
using Sale.Infrastructure.Interface;

namespace Sale.Api.Application.Handler.Contract
{
    public class ShowContractHandler : IRequestHandler<ShowContractCommand, ApiPaging<ContractDTO>>
    {
        private readonly IBaseRepository<ContractModel> contracrtRepo;
        private readonly ILogger<ShowContractHandler> logger;
        private readonly IBaseRepository<ClientModel> cilentRepo;
        private readonly IBaseRepository<UserModel> userRepo;

        public ShowContractHandler(IBaseRepository<ContractModel> contracrtRepo,ILogger<ShowContractHandler> logger,IBaseRepository<ClientModel> cilentRepo,IBaseRepository<UserModel> userRepo)
        {
            this.contracrtRepo = contracrtRepo;
            this.logger = logger;
            this.cilentRepo = cilentRepo;
            this.userRepo = userRepo;
        }
        public async Task<ApiPaging<ContractDTO>> Handle(ShowContractCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ContractDTO> result = new ApiPaging<ContractDTO>();
            var contract = contracrtRepo.GetAll().Where(x => x.IsDel == false);
            var user = userRepo.GetAll();
            var client = cilentRepo.GetAll().Where(x=>x.IsDel==false);
            if (!string.IsNullOrEmpty(request.ContractName))
            {
                contract = contract.Where(x => x.ContractName.Contains(request.ContractName));
            }
            if (!string.IsNullOrEmpty(request.ContractCode))
            {
                contract = contract.Where(x => x.ContractCode==request.ContractCode);
            }

            var linq = from a in contract
                       join b in client
                       on a.ClientId equals b.ClientId
                       join c in user
                       on a.LoginUserId equals c.UserId
                       select new ContractDTO
                       {
                           DeliveryAdder = a.DeliveryAdder,
                           DiscountAmount = a.DiscountAmount,
                           ClientId = a.ClientId,
                           ClientName=b.ClientName,
                           ContractCode=a.ContractCode,
                           ContractId=a.ContractId,
                           ContractName=a.ContractName,
                           ContractRemark=a.ContractRemark,
                           ShippingDate=a.ShippingDate,
                           SigningDate=a.SigningDate,
                           ContractState=a.ContractState,
                           LoginUserId=a.LoginUserId,
                           User_Name=c.UserName 
                       };

            result.Code = ApiEnum.OK;
            result.Data=linq.ToList();
            result.Msg = "销售单查询显示成功";
            logger.LogInformation(result.Msg);
            return await Task.FromResult(result);
        }
    }
}
